const http = require('http')

const server = http.createServer()

server.on('request', (req, res) => {
    // const str = `您访问的地址是：${req.url}，访问的方法是:${req.method}`

    // // 解决res返回给客户端中文乱码问题，将响应头的编码格式改为utf-8
    // res.setHeader('Content-Type', 'text/html; charset=utf-8')
    // res.end(str)
    let content = '<h1>404 Not Found</h1>'

    const url = req.url

    if(url === '/' || url === '/index.html') {
        content = '首页'
    }else if(url === '/about.html') {
        content = '关于'
    }

    res.setHeader('Content-Type', 'text/html; charset=utf-8')

    res.end(content)
})

server.listen(8080, 'localhost', () => {
    console.log('server is running at 127.0.0.1:8080')
})

